/*
*  gdialog.c - general dialog windows
*  Copyright (c) 2005 Derek Ingrouville (dergro@users.sourceforge.net)
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU Library General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <string.h>

#include "gdialog.h"
#include "msn/msn.h"

/* ---------- Display Messages ---------- */

void
vobri_display_message (GtkWindow *parent, const gchar *msg)
{
	GtkWidget *dialog = NULL;

	g_return_if_fail (msg != NULL);
	g_return_if_fail (strlen (msg) > 0);

	dialog = gtk_message_dialog_new (parent,
		GTK_DIALOG_DESTROY_WITH_PARENT,
		GTK_MESSAGE_INFO,
		GTK_BUTTONS_CLOSE, msg);

	gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);

	return;
}

void
vobri_display_warning (GtkWindow *parent, const gchar *warning)
{
	GtkWidget *dialog = NULL;

	g_return_if_fail (warning != NULL);
	g_return_if_fail (strlen (warning) > 0);

	dialog = gtk_message_dialog_new (parent,
		GTK_DIALOG_DESTROY_WITH_PARENT,
		GTK_MESSAGE_WARNING,
		GTK_BUTTONS_CLOSE, 
		warning);


	gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);

	return;
}

void
vobri_display_error (GtkWindow *parent, const gchar *error)
{
	GtkWidget *dialog = NULL;

	g_return_if_fail (error != NULL);
	g_return_if_fail (strlen (error) > 0);

	dialog = gtk_message_dialog_new (parent,
		GTK_DIALOG_DESTROY_WITH_PARENT,
		GTK_MESSAGE_ERROR,
		GTK_BUTTONS_CLOSE, 
		error);

	gtk_widget_show_all (GTK_WIDGET (dialog));

	gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);

	return;
}

gint
vobri_display_yesno (GtkWindow *parent, const gchar *question)
{
	GtkWidget *dialog = NULL;
	gint ret = 0;

	g_return_if_fail (question != NULL);
	g_return_if_fail (strlen (question) > 0);

	dialog = gtk_message_dialog_new (parent,
		GTK_DIALOG_DESTROY_WITH_PARENT,
		GTK_MESSAGE_QUESTION,
		GTK_BUTTONS_YES_NO, 
		question);

	gtk_widget_show_all (GTK_WIDGET (dialog));

	ret = gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);

	return ret;
}

/* ---------- Generic Input Dialog ---------- */

void 
draw_generic_input_dialog (GGenericInputDialog *input_dialog, 
			   const char *title, 
			   const char *msg)
{

	GtkWidget *dialog;
	GtkWidget *label;
	GtkEntry *entry; 
	GtkAlignment *align;

	dialog = gtk_dialog_new_with_buttons (title,
		NULL,
		GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
		GTK_STOCK_OK,
		GTK_RESPONSE_ACCEPT,
		GTK_STOCK_CANCEL,
		GTK_RESPONSE_REJECT,
		NULL);

	gtk_widget_set_size_request (dialog, 250, 110);
	gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);

	label = gtk_label_new (NULL);
	gtk_label_set_markup (GTK_LABEL (label), msg);
	gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);

	align = GTK_ALIGNMENT (gtk_alignment_new (0, 0, 0, 0));
	gtk_alignment_set_padding (GTK_ALIGNMENT (align), 5, 10, 2, 0);
	gtk_container_add (GTK_CONTAINER (align), label);

	entry = GTK_ENTRY (gtk_entry_new ());

	gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
		GTK_WIDGET (align));

	gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
		GTK_WIDGET (entry));

	input_dialog->dialog = GTK_DIALOG (dialog);
	input_dialog->entry = entry;

	gtk_widget_show_all (GTK_WIDGET (dialog));

	return;

}

/* ---------- Nickname Dialog ---------- */

GNicknameDialog *
nickname_dialog_new (MsnSession *session)
{
	GNicknameDialog *nick_dialog = NULL;
	char *title = "Change Display Name";
	char *msg = "<big><b>Change your Display Name</b></big>";

	g_return_val_if_fail (session != NULL, NULL);

	nick_dialog = g_new (GNicknameDialog, 1);

	nick_dialog->dialog = NULL;
	nick_dialog->entry = NULL;
	nick_dialog->session = session;

	draw_generic_input_dialog ((GGenericInputDialog *) nick_dialog,
				   title,
				   msg);

	gtk_entry_set_text (nick_dialog->entry, session->acct->nickname);

	return nick_dialog;
}

void
nickname_dialog_free (GNicknameDialog *nick_dialog)
{
	g_return_if_fail (nick_dialog != NULL);

	gtk_widget_destroy (GTK_WIDGET (nick_dialog->dialog));

	nick_dialog->dialog = NULL;
	nick_dialog->entry = NULL;
	nick_dialog->session = NULL;

	g_free (nick_dialog);

	return;
}

gchar *
nickname_dialog_run (GNicknameDialog *nick_dialog)
{
	gchar *nickname = NULL;
	gint result = 0;

	g_return_val_if_fail (nick_dialog != NULL, FALSE);

	result = gtk_dialog_run (GTK_DIALOG (nick_dialog->dialog));

	if (result == GTK_RESPONSE_ACCEPT)
		nickname = g_strdup (gtk_entry_get_text (nick_dialog->entry));

	return nickname;
}

/* ---------- SendIM Dialog ---------- */

GSendImDialog *
send_im_dialog_new ()
{
	GSendImDialog *imdialog = NULL;
	char *title = "Send Instant Message";
	char *msg = "<big><b>Enter the passport of the person you would like to instant message.</b></big>";

	imdialog = g_new (GSendImDialog, 1);

	imdialog->dialog = NULL;
	imdialog->entry = NULL;
	imdialog->session = NULL;

	draw_generic_input_dialog ((GGenericInputDialog *) imdialog,
				   title,
				   msg);

	return imdialog;
}

void
send_im_dialog_free (GSendImDialog *imdialog)
{
	g_return_if_fail (imdialog != NULL);

	gtk_widget_destroy (GTK_WIDGET (imdialog->dialog));

	imdialog->dialog = NULL;
	imdialog->entry = NULL;
	imdialog->session = NULL;

	g_free (imdialog);
}

gchar *
send_im_dialog_run (GSendImDialog *imdialog)
{
	
	char *passport = NULL;
	gint result = 0;

	g_return_val_if_fail (imdialog != NULL, NULL);

	result = gtk_dialog_run (GTK_DIALOG (imdialog->dialog));

	/* TODO: input validation */
	if (result == GTK_RESPONSE_ACCEPT)
		passport = g_strdup (gtk_entry_get_text (imdialog->entry));

	return passport;
}

/* ---------- Add Contact Dialog ---------- */

GContactAddDialog *
contact_add_dialog_new ()
{
	return NULL;
}

void
contact_add_dialog_free (GContactAddDialog *contactdialog)
{
}


MsnContact *
contact_add_dialog_run (GContactAddDialog *contactdialog)
{
	return NULL;
}

/* ---------- Add Group Dialog ---------- */

GGroupAddDialog *
group_add_dialog_new ()
{
	GGroupAddDialog *groupdialog = NULL;
	char *title = "Add a Group";
	char *msg = "<big><b>Group Name</b></big>";

	groupdialog = g_new (GSendImDialog, 1);

	groupdialog->dialog = NULL;
	groupdialog->entry = NULL;
	groupdialog->session = NULL;

	draw_generic_input_dialog ((GGenericInputDialog *) groupdialog,
				   title,
				   msg);

	return groupdialog;
}

void
group_add_dialog_free (GGroupAddDialog *groupdialog)
{
	g_return_if_fail (groupdialog != NULL);

	gtk_widget_destroy (GTK_WIDGET (groupdialog->dialog));

	groupdialog->dialog = NULL;
	groupdialog->entry = NULL;
	groupdialog->session = NULL;

	g_free (groupdialog);

	return;
}

gchar *
group_add_dialog_run (GGroupAddDialog *groupdialog)
{
	char *groupname = NULL;
	gint result = 0;

	g_return_val_if_fail (groupdialog != NULL, NULL);

	result = gtk_dialog_run (GTK_DIALOG (groupdialog->dialog));

	/* TODO: input validation */
	if (result == GTK_RESPONSE_ACCEPT)
		groupname = g_strdup (gtk_entry_get_text (groupdialog->entry));

	return groupname;
}